﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Arizu.Repository;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace AiRiZu.User
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    public class UpdateImg : IHttpHandler, IRequiresSessionState   
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Session["logId"] == null)
            {
                return;
            }

            AirizuUser user = AirizuUser.CreateInstance();

            int x1 = int.Parse(context.Request.Params["x1"].ToString());
            int x2 = int.Parse(context.Request.Params["x2"].ToString());
            int y1 = int.Parse(context.Request.Params["y1"].ToString());
            int y2 = int.Parse(context.Request.Params["y2"].ToString());
            string url = context.Request.Params["url"].ToString().Split(',')[0];
            url = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "user/" + url;
            Bitmap b = new Bitmap(url);

            b = KiCut(b, x1, y1, x2, y2);

            FileInfo file = new FileInfo(url);
            string tmpName = System.IO.Path.GetTempFileName();
            tmpName = tmpName.Substring(tmpName.LastIndexOf("\\") + 1, tmpName.Length - (tmpName.LastIndexOf("\\") + 1)).Replace(".tmp", "");
            string fileName = tmpName + file.Extension;
            b.Save(file.DirectoryName + "/" + fileName);

            user.UpdateImgName(int.Parse(context.Session["logId"].ToString()), fileName);

            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }

            int w = b.Width;
            int h = b.Height;

            if (StartX >= w || StartY >= h)
            {
                return null;
            }

            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }

            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }

            try
            {
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);

                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();

                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
    }
}
